package com.itheima.controller;

import com.itheima.common.constant.RedisConstant;
import com.itheima.properties.FreeMarkerProperties;
import com.itheima.sevice.SetmealService;
import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.AliOssUtils;
import com.itheima.pojo.Setmeal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private AliOssUtils aliOssUtil;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增套餐
     * @param checkgroupIds
     * @param setmeal
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestParam List<Integer> checkgroupIds, @RequestBody Setmeal setmeal) throws Exception {
        Integer recordCount = setmealService.add(checkgroupIds, setmeal);

        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS, recordCount);
    }

    /**
     * 编辑套餐
     * @param checkGroupIds
     * @param setmeal
     * @return
     */
    @PostMapping("/edit")
    public Result edit(@RequestParam List<Integer> checkGroupIds, @RequestBody Setmeal setmeal) throws Exception {
        Integer recordCount = setmealService.edit(checkGroupIds, setmeal);

        return new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS, recordCount);
    }

    /**
     * 根据id查询套餐数据
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(Integer id){
        Setmeal setmeal = setmealService.findById(id);

        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, setmeal);
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = setmealService.findPage(queryPageBean);

        return pageResult;
    }

    @PostMapping("/upload")
    public Result upload(MultipartFile imgFile){
        try {
            String fileName = imgFile.getOriginalFilename();
            String extName = fileName.substring(fileName.lastIndexOf("."));
            UUID uuid = UUID.randomUUID();
            String newFileName = uuid + extName;
            aliOssUtil.upload(imgFile.getBytes(), newFileName);
            // 将上传的图片名存储在redis中, 用set类型对象进行存储
            redisTemplate.opsForSet().add(RedisConstant.SETMEAL_PIC_RESOURCES, newFileName);

            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, newFileName);
        } catch (Exception e) {
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }
}
